<template>
  <el-card style="margin-bottom: 10px">
    <!-- 表单 -->
    <el-form inline label-width="100px">
      <el-form-item label="一级分类">
        <el-select v-model="category1Id" placeholder="请选择" :disabled="disabled">
          <el-option v-for="c1 in categoryStore._category1List" :key="c1.id" :label="c1.name" :value="c1.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select v-model="category2Id" placeholder="请选择" :disabled="disabled">
          <el-option v-for="c2 in categoryStore._category2List" :key="c2.id" :label="c2.name" :value="c2.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select v-model="categoryStore.category3Id" placeholder="请选择" :disabled="disabled">
          <el-option v-for="c3 in categoryStore._category3List" :key="c3.id" :label="c3.name" :value="c3.id" />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script lang="ts">
export default {
  name: 'CategorySelector'
}
</script>
<script lang="ts" setup>
import {ref, onMounted, computed, watch} from 'vue'
// 引入分类仓库
import {useCategoryStore} from '@/stores/category'
const categoryStore = useCategoryStore()
// 引入Spu组件显示状态标识
import {SpuStatus} from '@/views/product/spu/SpuStatus'

// 接收父组件传递的参数
const props = defineProps(['isShow'])
const disabled = computed(() => {
  return props.isShow !== SpuStatus.Spu_List
})

// 计算属性 一级分类
const category1Id = computed({
  get() {
    return categoryStore._category1Id as number
  },
  set(val: number) {
    // 获取二级分类列表时会更新一级id
    categoryStore.getCategory2List(val)
  }
})
// 计算属性 二级分类
const category2Id = computed({
  get() {
    return categoryStore._category2Id as number
  },
  set(val: number) {
    // 获取三级分类列表时会更新二级id
    categoryStore.getCategory3List(val)
  }
})

// 组件挂载完成
onMounted(() => {
  // 请求一级分类列表数据
  categoryStore.getCategory1List()
})
</script>
<style scoped>
</style>